﻿using System;
using System.Collections.Generic;
using WindowsGame.Data;
using WindowsGame.Enums;
using WindowsGame.GameObjects;
using WindowsGame.Static;
using WindowsGame.Library.Data;
using WindowsGame.Library.IoC;
using WindowsGame.Tests.Data;
using NUnit.Framework;

namespace WindowsGame.Tests
{
	[TestFixture]
	public class FileManagerTests
	{
		// System under test.
		private IFileManager fileManager;

		[TestFixtureSetUp]
		public void TestFixtureSetUp()
		{
			IoCContainer.Initialize<IFileProxy, TestFileProxy>();
			IoCContainer.Initialize<IFileManager, FileManager>();
		}

		[SetUp]
		public void SetUp()
		{
			fileManager = IoCContainer.Resolve<IFileManager>();
		}

		[Test]
		public void BoardDataTest()
		{
			String boardFile = GetPath("BoardData.txt");

			IList<String> lines = fileManager.LoadTxt(boardFile);
			String line = lines[0];
	
			GameSize gameSize = (GameSize)Enum.Parse(typeof(GameSize), line, true);
			Assert.AreEqual(GameSize.Small, gameSize);
		}

		[Test]
		public void SnakeDataTest()
		{
			String snakeFile = GetPath("SnakeData.xml");
			SnakeData[] snakeData = fileManager.LoadXml<SnakeData[]>(snakeFile);
			Assert.AreEqual(8, snakeData.Length);

			SnakeData snake = snakeData[0];
			Assert.AreEqual(10, snake.StartPosX);
			Assert.AreEqual(5, snake.StartPosY);
			Assert.AreEqual(Direction.Left, snake.Direction);
			Assert.AreEqual(20, snake.TailLength);
			Assert.AreEqual(50, snake.TimeOnTile);
			Assert.AreEqual(10, snake.TileToMove);
		}

		private static String GetPath(String name)
		{
			return GetPath(Constants.DATA_DIRECTORY, name);
		}
		private static String GetPath(String fldr, String name)
		{
			String root = @"..\..\..\..\WindowsGame\WindowsGame\bin\x86\Debug\";
			String path = String.Format("{0}/{1}/{2}", Constants.ROOT_DIRECTORY, fldr, name);

			return String.Format("{0}{1}", root, path);
		}

		[TestFixtureTearDown]
		public void TestFixtureTearDown()
		{
			IoCContainer.Release();
		}
	}
}